/*
 * @Author: wangtao
 * @Date: 2020-06-29 16:32:41
 * @LastEditors: 汪滔
 * @LastEditTime: 2021-05-28 17:41:15
 * @Description: file content
 */

import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Modal,
  Text,
  TouchableOpacity,
  PixelRatio,
  ScrollView,
} from 'react-native';
import {
  mainBgColorLightGray,
  mainBgColorWhite,
  priceColor,
  fontColorBlack,
  fontColorLightGray,
  fontColorCoffee,
  screenWidth,
  px2dp,
  screenHeight,
  splitLineColorLightGray,
  fontColorSecDeepGray,
  mainBgColorBlack,
} from '@/styles';
import { Button, msg } from '@/common';
import SelectItem from './select-item';

const { Submit } = Button;
/**
 * @author kanglang
 * @description 车型选择框
 * @createDate 2020/7/27
 *
 */
export default class TruckTypeSelect extends Component {
  constructor(props) {
    super(props);
    this.state = {
      truckType: '',
      transportation: this.props.transportation
    };
  }

  componentDidMount() {
    // 回显
    const { truckType } = this.props;
    this.setState({ truckType });
  }

  render() {
    const { transportation } = this.state;
    const { onHideModal, alertTitle = '标题', sourceType, alertType } = this.props;

    return (
      <Modal
        animationType='slide'
        transparent
        visible
        onRequestClose={() => {
          onHideModal();
        }}
        onShow={() => { }}
      >
        <TouchableOpacity
          style={styles.mask}
          onPress={() => {
            onHideModal();
          }}
        />
        <View style={styles.infoContainer}>
          <View style={styles.header}>
            <TouchableOpacity
              style={styles.cancel}
              onPress={() => {
                onHideModal();
              }}
            >
              <Text style={{ fontSize: px2dp(32), color: fontColorSecDeepGray }}>
                取消
              </Text>
            </TouchableOpacity>
            <Text
              style={{
                fontSize: px2dp(32),
                color: fontColorBlack,
                fontWeight: 'bold',
              }}
            >
              {alertTitle}
            </Text>
            <Submit
              text='确定'
              boxStyle={{
                width: px2dp(104),
                height: px2dp(60),
                borderRadius: px2dp(8),
                position: 'absolute',
                right: px2dp(28),
              }}
              disabled={false}
              onClick={() => {
                this.submit();
              }}
            />
          </View>
          <ScrollView style={styles.editContainer} showsVerticalScrollIndicator={false}>
            <View style={styles.packing}>
              <View style={styles.packingSelect}>
                {
                  sourceType && sourceType.map((item, index) => {
                    return <SelectItem
                      name={item.name}
                      type={alertType}
                      value={item.id}
                      activeVal={transportation}
                      onChange={(type, value, name) => this.onChange(type, value, name)}
                    />;
                  })
                }
              </View>
            </View>
          </ScrollView>
        </View>
      </Modal>
    );
  }

    // 改变
    onChange = (type, val, name) => {
      // console.log("type----", type, "val--->", val, "name--->", name)
      this.setState({ [type]: val, truckType: name });
    };

    // 提交
    submit = () => {
      const { transportation, truckType } = this.state;
      const { onHideModal, onChangeVal } = this.props;
      onChangeVal({
        transportation,
        truckType,
      });
      onHideModal();
    };
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: mainBgColorLightGray,
    justifyContent: 'center',
  },
  mask: {
    width: screenWidth,
    height: screenHeight,
    backgroundColor: 'rgba(0,0,0,0.2)',
    position: 'absolute',
    left: 0,
    top: 0,
  },
  infoContainer: {
    width: screenWidth,
    maxHeight: 0.6 * screenHeight,
    backgroundColor: mainBgColorWhite,
    position: 'absolute',
    bottom: 0,
    left: 0,
    borderRadius: px2dp(12),
    overflow: 'hidden',
    zIndex: 10,
  },
  header: {
    width: screenWidth,
    height: px2dp(108),
    justifyContent: 'center',
    alignItems: 'center',
    borderBottomWidth: 1 / PixelRatio.get(),
    borderColor: splitLineColorLightGray,
  },
  cancel: {
    position: 'absolute',
    left: 0,
    top: 0,
    width: 80,
    height: '100%',
    justifyContent: 'center',
    alignItems: 'center',
  },
  packing: {
    marginTop: px2dp(48),
  },
  packingSelect: {
    marginTop: px2dp(24),
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'flex-start',
  },
  selectItem: {
    width: px2dp(218),
    height: px2dp(72),
    backgroundColor: mainBgColorLightGray,
    borderRadius: px2dp(8),
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: px2dp(16),
    marginRight: px2dp(10),
  },
  editContainer: {
    paddingHorizontal: px2dp(32),
    paddingBottom: px2dp(100),
  },
});
